home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snpd1292.zip / WHICHARC.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  3KB  |  93 lines

  1. /*
  2.    --------------------------------------------------------------------
  3.    Module:     WHICHARC.C
  4.    Subject:    tries to determine the archiver used to compress files
  5.    Author:     Heinz Ozwirk
  6.                modified for SNIPPETS by Bob Stout
  7.    Status:     public domain
  8.    Started:    28.09.1991   13:35:57
  9.    Modified:   13.10.1991   14:15:57
  10.    --------------------------------------------------------------------
  11.    Prototype:  int WhichArc(char *pName)
  12.       pName    address of full path name of file to examine
  13.       Result   -1:      file not found
  14.                UNKNOWN: unknown packer
  15.                ARC:     ARC or PKARC
  16.                ARJ:     ARJ
  17.                LHA:     LHARC or LHA
  18.                ZIP:     PKZIP
  19.                ZOO:     Zoo
  20.  
  21.    LHARC/LHA
  22.       No archive header. WhichArc examines the checksum of the first
  23.       file header. If the checksum is valid and if the string -lh?-
  24.       is found, LHA or LHARC is assumed.
  25.  
  26.    ARJ
  27.       If a file starts with 0x60, 0xEA, ARJ is assumed.
  28.  
  29.    ZIP
  30.       If the file begins with "PK", PKZIP is assumed.
  31.  
  32.    ZOO
  33.       Zoo'ed archives always start with "ZOO x.xx Archive". WhichArc
  34.       only looks for "ZOO".
  35.  
  36.    ARC
  37.       No header. Files starting with 0x1A are assumed to be ARCed.
  38.    --------------------------------------------------------------------
  39. */
  40.  
  41. #include <stdio.h>
  42. #include <string.h>
  43.  
  44. typedef unsigned char BYTE;
  45.  
  46. #define strNcmp(s1,s2,n) !strncmp((const char *)(s1), (s2), (n))
  47.  
  48. enum {UNKNOWN, ARC, ZOO, ARJ, LHARC, LHA, ZIP}
  49.  
  50. WhichArc(char *pName)
  51. {
  52.       FILE  *fp;
  53.       BYTE  header[128];
  54.       int   c, i, n;
  55.  
  56.       memset(header, 0, sizeof(header));
  57.       fp = fopen(pName, "rb");
  58.       if (fp == NULL)
  59.             return -1;
  60.       n = fread(header, sizeof(BYTE), sizeof(header) - sizeof(BYTE), fp);
  61.       fclose(fp);
  62.  
  63.       if (n <= 0)
  64.             return -1;
  65.  
  66.       if (n >= 7 && n >= header[0] + 2)
  67.       {
  68.             for (c = 0, i = header[0]; i--; c += (header+2)[i])
  69.                   ;
  70.             if ((c & 0x00FF) == header[1]       &&
  71.                   strNcmp(&header[2], "-lh", 3) &&
  72.                   '-' == header[6])
  73.             {
  74.                   return (header[5] > '1') ? LHA : LHARC;
  75.             }
  76.       }
  77.  
  78.       if (n >= 2)
  79.       {
  80.             if (strNcmp(header, "\x60\xEA", 2))
  81.                   return ARJ;
  82.             if (strNcmp(header, "PK", 2))
  83.                   return ZIP;
  84.       }
  85.  
  86.       if (n >= 3 && strNcmp(header, "ZOO", 3))
  87.             return ZOO;
  88.  
  89.       if (n >= 25 && *header == 0x1A) return ARC;
  90.  
  91.       return UNKNOWN;
  92. }
  93.